package com.example.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.boot.pojo.MxGroupPersonnelEntity;
import com.example.boot.pojo.MxUserGroupEntity;
import com.example.boot.pojo.Users;
import com.example.boot.service.MxGroupPersonnelService;
import com.example.boot.service.MxUserGroupService;
import com.example.boot.service.UserService;
import com.example.boot.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 我的群
 *
 * @author yy
 * @email yangxxil01@sina.com
 * @date 2022-01-08 14:35:28
 */
@RestController
@RequestMapping("mxusergroup")
public class MxUserGroupController {
    @Autowired
    private MxUserGroupService mxUserGroupService;
    @Autowired
    private MxGroupPersonnelService mxGroupPersonnelService;
    @Autowired
    private UserService userService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Long userId){
        LambdaQueryWrapper<MxUserGroupEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MxUserGroupEntity::getUserId,userId);
        return R.ok(mxUserGroupService.list(lambdaQueryWrapper));

    }
    @RequestMapping("/getUserGroup")
    public R getUserGroup(@RequestParam Long userId){

        return R.ok(mxUserGroupService.getUserGroup(userId));

    }



    /**
     * 信息
     */
    @RequestMapping("/info/{userGroupId}")
    public R info(@PathVariable("userGroupId") Long userGroupId){
		MxUserGroupEntity mxUserGroup = mxUserGroupService.getById(userGroupId);

        return R.ok().put("mxUserGroup", mxUserGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MxUserGroupEntity mxUserGroup){

        Long userId=mxUserGroup.getUserId();

        Users  user= userService.getById(userId);
        MxGroupPersonnelEntity mxGroupPersonnel =new MxGroupPersonnelEntity();
        mxGroupPersonnel.setGroupId(mxUserGroup.getGroupId());
        mxGroupPersonnel.setUserId(mxUserGroup.getUserId());
        mxGroupPersonnel.setPersonnelName(user.getNickname());
        LambdaQueryWrapper<MxUserGroupEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MxUserGroupEntity::getUserId,mxUserGroup.getUserId())
                        .eq(MxUserGroupEntity::getGroupId,mxUserGroup.getGroupId());
       List<MxUserGroupEntity> list= mxUserGroupService.list(lambdaQueryWrapper);
       if (list.size()>0){
           return R.error("已经添加了这个群");
       }
        mxGroupPersonnelService.save(mxGroupPersonnel);
        mxUserGroupService.save(mxUserGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MxUserGroupEntity mxUserGroup){
		mxUserGroupService.updateById(mxUserGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody MxUserGroupEntity mxUserGroup){
        LambdaQueryWrapper<MxUserGroupEntity> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(MxUserGroupEntity::getGroupId,mxUserGroup.getGroupId())
                .eq(MxUserGroupEntity::getUserId,mxUserGroup.getUserId());
LambdaQueryWrapper<MxGroupPersonnelEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MxGroupPersonnelEntity::getGroupId,mxUserGroup.getGroupId())
                .eq(MxGroupPersonnelEntity::getUserId,mxUserGroup.getUserId());
        mxUserGroupService.remove(lambdaQueryWrapper1);
        mxGroupPersonnelService.remove(lambdaQueryWrapper);
        return R.ok();
    }




}
